<?php

class ModelCfNews extends Model {
    
    /**
     * Add new record
     *
     * @param       array
     * @return      void
     * @access      public
     */
	public function addNews($data)
    {
        $this->db->query('INSERT INTO ' . DB_PREFIX . 'news SET status = ' . (int)$data['status'] . ', date_modified = NOW(), date_added = NOW()');
        $news_id = $this->db->getLastId();
        
        if (isset($data['image'])) {
            $sql = sprintf("UPDATE %snews SET image = '%s' WHERE news_id = '%s'", DB_PREFIX, $this->db->escape($data['image']), (int)$news_id);            
            $this->db->query($sql);
        }
        
        foreach ($data['news'] as $language_id => $value) {
            $sql = sprintf("INSERT %snews_description SET news_id = %s, language_id = %s, title = '%s', description = '%s', content = '%s'", 
                           DB_PREFIX, $news_id, (int)$language_id, $this->db->escape($value['title']), 
                           $this->db->escape($value['description']), $this->db->escape($value['content']));
            $this->db->query($sql);
        }
	}
	
	public function editNews($news_id, $data) {
        $this->db->query('UPDATE ' . DB_PREFIX . 'news SET status = ' . (int)$data['status'] . ', date_modified = NOW() WHERE news_id = ' .(int)$news_id);
                                                                                              
        if (isset($data['image'])) {
            $this->db->query("UPDATE " . DB_PREFIX . "news SET image = '" . $this->db->escape($data['image']) . "' WHERE news_id = '" . (int)$news_id . "'");
        }
        
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = " . (int)$news_id);
        
        foreach ($data['news'] as $language_id => $value) {
            $sql = sprintf("INSERT %snews_description SET news_id = %s, language_id = %s, title = '%s', description = '%s', content = '%s'", 
                           DB_PREFIX, $news_id, (int)$language_id, $this->db->escape($value['title']), 
                           $this->db->escape($value['description']), $this->db->escape($value['content']));
            $this->db->query($sql);
        }
	}
	
	public function deleteNews($news_id) {        
		$this->db->query("DELETE FROM " . DB_PREFIX . "news WHERE news_id = " . (int)$news_id);
        $this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = " . (int)$news_id);
	}
	
	public function getItem($news_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "news WHERE news_id = " . (int)$news_id);
		
        return $query->row;
	}

	public function getNews($data = array()) {
        $sql = sprintf('SELECT * FROM %snews n LEFT JOIN %snews_description nd ON n.news_id = nd.news_id WHERE nd.language_id = %s ', 
                       DB_PREFIX, DB_PREFIX, DEFAULT_LANG);
		
		$sort_data = array('title', 'date_added');	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY date_added";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}																																							  
																																							  
		$query = $this->db->query($sql);																																				
		
		return $query->rows;	
	}
	
    public function getNewsDetail($news_id)
    {
        $news_detail_data = array();
        
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_description WHERE news_id = " . (int)$news_id);
        
        foreach ($query->rows as $result) {
            $news_detail_data[$result['language_id']] = array(
                'title'         => $result['title'],
                'description'   => $result['description'],
                'content'       => $result['content']
            );
        }
        
        return $news_detail_data;
    }
    
	public function getTotalNews() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "news");
		
		return $query->row['total'];
	}
	
}
?>